<?php
// +----------------------------------------------------------------------
// | User: zq
// +----------------------------------------------------------------------
// | Time: 2021/12/21 13:25
// +----------------------------------------------------------------------

namespace App\Http\Controllers\Wx;

use App\Http\Controllers\Controller;
use App\Models\Test\Test1;
use App\Models\Test\Test2;
use App\Models\Test\Test3;

class TestController extends Controller
{
    private $number = '100';

    public function test1()
    {
        $start = microtime(true);

        $list = Test1::query()->where('id', '<', $this->number)->get();
        foreach ($list as $item) {
            $item['test2'] = Test2::query()->find($item->test2_id);
            $item['test3'] = Test3::query()->find($item->test3_id);
        }

        $time = microtime(true) - $start;
        dd($time);
    }

    public function test2()
    {
        $start = microtime(true);

        $list = Test1::query()->where('id', '<', $this->number)->get();
        $test2Ids = $list->pluck('test2_id')->toArray();
        $test2List = Test2::query()->whereIn('id', $test2Ids)->get()->keyBy('id');
        $test3Ids = $list->pluck('test3_id')->toArray();
        $test3List = Test3::query()->whereIn('id', $test3Ids)->get()->keyBy('id');

        foreach ($list as $item) {
            $item['test2'] = $test2List->get($item->test2_id);
            $item['test3'] = $test3List->get($item->test3_id);
        }

        $time = microtime(true) - $start;
        dd($time);
    }
}